# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================



# ==================================
# Location of LDP and DPJ candidates against entire distribution of
# candidate positions, 1996-2009
# (the paper's Figure 5)

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

all.96 <- ideal[ideal$year==1996,]
dpj.96 <- ideal[ideal$year==1996 & ideal$pty==52,]
ldp.96 <- ideal[ideal$year==1996 & ideal$pty==1,]

all.00 <- ideal[ideal$year==2000,]
dpj.00 <- ideal[ideal$year==2000 & ideal$pty==52,]
ldp.00 <- ideal[ideal$year==2000 & ideal$pty==1,]

all.03 <- ideal[ideal$year==2003,]
dpj.03 <- ideal[ideal$year==2003 & ideal$pty==52,]
ldp.03 <- ideal[ideal$year==2003 & ideal$pty==1,]

all.05 <- ideal[ideal$year==2005,]
dpj.05 <- ideal[ideal$year==2005 & ideal$pty==52,]
ldp.05 <- ideal[ideal$year==2005 & ideal$pty==1,]

all.09 <- ideal[ideal$year==2009,]
dpj.09 <- ideal[ideal$year==2009 & ideal$pty==52,]
ldp.09 <- ideal[ideal$year==2009 & ideal$pty==1,]

par(mfrow = c(2, 3), mar = c(4,4,2,1), tcl = -0.25, mgp = c(1.75, 0.6, 0),
    font.main = 1, cex.main = 2)

dens.96 <- density(all.96$sep_theta)
range(all.96$sep_theta)
dens.00 <- density(all.00$sep_theta)
range(all.00$sep_theta)
dens.03 <- density(all.03$sep_theta)
range(all.03$sep_theta)
dens.05 <- density(all.05$sep_theta)
range(all.05$sep_theta)
dens.09 <- density(all.09$sep_theta)
range(all.09$sep_theta)

plot(dens.96, main="1996 Positions", xlim=c(-3,3), ylim=c(0, 0.61), cex.axis=1.5, cex.lab=1.5)
rug(dpj.96$sep_theta, side=1, line = 0, col="grey64", lwd=3, tck=0.03)
rug(ldp.96$sep_theta, side=1, line = 0.5, col="grey41", lwd=3, tck=0.03)
Axis(at = mean(dpj.96$sep_theta), line = 0, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)
Axis(at = mean(ldp.96$sep_theta), line = 0.5, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)

plot(dens.00, main="2000 Positions", xlim=c(-3,3), ylim=c(0, 0.61), cex.axis=1.5, cex.lab=1.5)
rug(dpj.00$sep_theta, side=1, line = 0, col="grey64", lwd=3, tck=0.03)
rug(ldp.00$sep_theta, side=1, line = 0.5, col="grey41", lwd=3, tck=0.03)
Axis(at = mean(dpj.00$sep_theta), line = 0, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)
Axis(at = mean(ldp.00$sep_theta), line = 0.5, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)

plot(dens.03, main="2003 Positions", xlim=c(-3,3), ylim=c(0, 0.61), cex.axis=1.5, cex.lab=1.5)
rug(dpj.03$sep_theta, side=1, line = 0, col="grey64", lwd=3, tck=0.03)
rug(ldp.03$sep_theta, side=1, line = 0.5, col="grey41", lwd=3, tck=0.03)
Axis(at = mean(dpj.03$sep_theta), line = 0, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)
Axis(at = mean(ldp.03$sep_theta), line = 0.5, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)

plot(dens.05, main="2005 Positions", xlim=c(-3,3), ylim=c(0, 0.61), cex.axis=1.5, cex.lab=1.5)
rug(dpj.05$sep_theta, side=1, line = 0, col="grey64", lwd=3, tck=0.03)
rug(ldp.05$sep_theta, side=1, line = 0.5, col="grey41", lwd=3, tck=0.03)
Axis(at = mean(dpj.05$sep_theta), line = 0, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)
Axis(at = mean(ldp.05$sep_theta), line = 0.5, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)

plot(dens.09, main="2009 Positions", xlim=c(-3,3), ylim=c(0, 0.61), cex.axis=1.5, cex.lab=1.5)
rug(dpj.09$sep_theta, side=1, line = 0, col="grey64", lwd=3, tck=0.03)
rug(ldp.09$sep_theta, side=1, line = 0.5, col="grey41", lwd=3, tck=0.03)
Axis(at = mean(dpj.09$sep_theta), line = 0, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)
Axis(at = mean(ldp.09$sep_theta), line = 0.5, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)

rm(list=ls())


# ==================================
# Increase in within-party dispersion in 2009 due to increase in within-DPJ dispersion

load("party_dispersion_prepostER.Rdata") # called dat2a

# Dispersion in DPJ candidate positions in elections, 1996-2009
dpj.data <- dat2a[dat2a$pty=="dpj",]
dpj.data

# Dispersion in positions of candidates from all parties, 2009
data.2009 <- dat2a[dat2a$year==2009,]
data.2009

# Dispersion in positions of candidates from all parties, 1996-2009
# excluding itself in 2009
data.all <- dat2a[!(dat2a$pty=="dpj" & dat2a$year==2009),]
data.all.new <- data.all[data.all$year>1995,]
range(data.all.new$var)

rm(list=ls())




# ==================================
# Increased variance in DPJ candidate positions in 2009 is not explained
# by new DPJ candidates:

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

dpj.05 <- ideal[ideal$year==2005 & ideal$pty==52,]
dpj.09 <- ideal[ideal$year==2009 & ideal$pty==52,]

dpj.05.a <- dpj.05[(dpj.05$"���O" %in% dpj.09$"���O"),]
dpj.09.a <- dpj.09[(dpj.09$"���O" %in% dpj.05$"���O"),]
so.dpj.05.a <- dpj.05.a[order(dpj.05.a[,3]),]
so.dpj.09.a <- dpj.09.a[order(dpj.09.a[,3]),]
identical(so.dpj.05.a$���O, so.dpj.09.a$���O)
so.dpj.09.a$ku_05 <- so.dpj.05.a$ku
so.dpj.09.a$same.ku <- ifelse(so.dpj.09.a$ku==so.dpj.09.a$ku_05, 1, 0)
new.09 <- so.dpj.09.a[so.dpj.09.a$same.ku==1,]
new.05 <- so.dpj.05.a[so.dpj.05.a$���O %in% new.09$���O,] 
# both have 178 DPJ candidates

# Variance in their positions in 2005?
var(new.05$sep_theta) 

# Variance in their positions in 2009?
var(new.09$sep_theta) 

# Variance in positions of all DPJ candidates in 2009:
var(dpj.09$sep_theta)

var.test(new.09$sep_theta, dpj.09$sep_theta) 

rm(list=ls())




# ==================================
# Increased distance from LDP opponents in 2009 not explained by 
# new DPJ-LDP pairings.

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

# Calculate absolute distances between LDP and DPJ candidates in
# all elections, 1996-2009
ideal.points <- ideal[ideal$year>1993 & (ideal$pty==1|ideal$pty==52),]
years <- unique(ideal.points$year)
all.the.years <- matrix(as.character(NA), ncol=5, nrow=0)
for(j in 1:length(years)){
  year.dset <- ideal.points[ideal.points$year==years[j],]
  kus <- unique(year.dset$ku)
  tops <- matrix(as.character(NA), ncol=5, nrow=0)
  for(i in 1:length(kus)){
    subku <- year.dset[year.dset$ku==kus[i],]
    if(nrow(subku)>1){
      ldp <- subku[subku$pty==1,]
      pos.ldp <- ldp$sep_theta
      ldp.cand <- ldp$���O
      dpj <- subku[subku$pty==52,]
      pos.dpj <- dpj$sep_theta
      dpj.cand <- dpj$���O
      dist1 <- abs(pos.ldp - pos.dpj)
      tops <- rbind(tops, cbind(unique(subku$year), 
                                unique(subku$ku), 
                                as.character(ldp.cand), 
                                as.character(dpj.cand),
                                dist1))
  } else next
  }
all.the.years <- rbind(all.the.years, tops)
}
colnames(all.the.years) <- c("year", "ku", "ldp_cand", "dpj_cand", "abs.dist")
sorted <- all.the.years[order(all.the.years[,1], all.the.years[,2]),]
sorted <- as.data.frame(sorted)
sorted$pair <- paste(sorted[,3], sorted[,4], sep="_")

# Mean abs dist between same-district LDP and DPJ cand in 2005:
dat.05 <- sorted[sorted$year==2005,]
nrow(dat.05)
dat.05$abs.dist <- as.numeric(as.character(dat.05$abs.dist))
mean(dat.05$abs.dist)

# Mean abs dist between same-district LDP and DPJ cand in 2009:
dat.09 <- sorted[sorted$year==2009,]
nrow(dat.09)
dat.09$abs.dist <- as.numeric(as.character(dat.09$abs.dist))

t.test(dat.05$abs.dist, dat.09$abs.dist)

# Restrict 2009 data to cand pairs who also competed against each other in 2005:
dat.09p <- dat.09[dat.09$pair %in% dat.05$pair,]
nrow(dat.09p)
# Restrict 2005 data to cand pairs who also competed against each other in 2009:
dat.05p <- dat.05[dat.05$pair %in% dat.09$pair,]

# Are these 142 pairs further away in 2009 relative to 2005?
t.test(dat.09p$abs.dist, dat.05p$abs.dist)

# Are these 142 pairs closer than the universe of LDP-DPJ candidate pairings in 2009?
t.test(dat.09$abs.dist, dat.09p$abs.dist)

save(dat.09p, file="LDP_DPJ_candpairs_in_2005&2009.Rdata")

rm(list=ls())






# ==================================
# Location of same DPJ and LDP foes against distribution of candidate positions, 2005-2009
# (the paper's Figure 6)

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

all.05 <- ideal[ideal$year==2005,]
all.09 <- ideal[ideal$year==2009,]

# get list of LDP-DPJ pairs who competed against each other in both 2005 and 2009
load("LDP_DPJ_candpairs_in_2005&2009.Rdata")

# Subset 2005 data by these 142 pairings:
dpj.05 <- all.05[all.05$���O %in% dat.09p$dpj_cand,]
ldp.05 <- all.05[all.05$���O %in% dat.09p$ldp_cand,]

# Subset 2009 data by these 142 pairings:
dpj.09 <- all.09[all.09$���O %in% dat.09p$dpj_cand,]
ldp.09 <- all.09[all.09$���O %in% dat.09p$ldp_cand,]

par(mfrow = c(1,2), mar = c(4,4,2,1), tcl = -0.25, mgp = c(1.75, 0.6, 0),
    font.main = 1, cex.main = 2)
dens.05 <- density(all.05$sep_theta)
plot(dens.05, main = "2005 Positions", xlim=c(-3,3), ylim=c(0, 0.61), cex.axis=1.5, cex.lab=1.5)
rug(dpj.05$sep_theta, side=1, line = 0, col="grey64", lwd=3, tck=0.03)
rug(ldp.05$sep_theta, side=1, line = 0.5, col="grey41", lwd=3, tck=0.03)
Axis(at = mean(dpj.05$sep_theta), line = 0, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)
Axis(at = mean(ldp.05$sep_theta), line = 0.5, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)

dens.09 <- density(all.09$sep_theta)
plot(dens.09, main = "2009 Positions", xlim=c(-3,3), ylim=c(0, 0.61), cex.axis=1.5, cex.lab=1.5)
rug(dpj.09$sep_theta, side=1, line = 0, col="grey64", lwd=3, tck=0.03)
rug(ldp.09$sep_theta, side=1, line = 0.5, col="grey41", lwd=3, tck=0.03)
Axis(at = mean(dpj.09$sep_theta), line = 0, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)
Axis(at = mean(ldp.09$sep_theta), line = 0.5, side = 1, labels = "", col = "black", 
     lwd.ticks = 3, tck = 0.03)

rm(list=ls())




# ==================================
# Regression of absolute distances between DPJ and LDP candidate in 2009
# on variables measuring security and ideological leaning.
# (Table 4 in Appendix)

load("LDP_DPJ_abs_dists_2009.Rdata") # saved as mer3

# Variables:
# abs dists = absolute distance between positions of all 263 same-district DPJ-LDP pairings in 2009
# ldp.co == 1 (DPJ candidate is facing an LDP candidate who won in the same district in 2003 and 2005)
# dpj_prev.runs = number of times DPJ cand has run since 1986
# former.ldp = 1 (ran from LDP in a previous election)
# former.jsp = 1 (ran from JSP in previos election)
# former.ozawa = 1 (ran from ozawa iberals in a previous election)
# repeat.pair = 1 (the candidates ran against each other in 2005)
# female = 1 (DPJ cand is female)

mer3$ldp.co <- as.factor(mer3$ldp.co)
mer3$dpj_female <- as.factor(mer3$dpj_female)
mer3$former.ldp <- as.factor(mer3$former.ldp)
mer3$former.jsp <- as.factor(mer3$former.jsp)
mer3$former.ozawa <- as.factor(mer3$former.ozawa)
mer3$ku <- as.factor(mer3$ku)

fit3 <- lm(abs.dist ~ ldp.co +
             dpj_prev.runs +
             ldp.co*dpj_prev.runs +
             former.ldp +
             former.jsp + 
             former.ozawa +
             prefecture +
             ur +
             dpj_female +
             dpj_age +
             repeat.pair,
           data=mer3)
summary(fit3)


rm(list=ls())
